﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Control.Bus;

namespace QuanLyKho
{
    public partial class FrmQLKe : Form
    {
        List<BusShelf> shefts = new List<BusShelf>();
        List<BusShelf> ShelfsdisplayGrid = new List<BusShelf>();
        List<BusShelf> sheftstoupdate = new List<BusShelf>();
        List<BusShelf> sheftstodelete = new List<BusShelf>();
        public FrmQLKe()
        {
            InitializeComponent();
            
            shefts = BusShelf.GetAll();
            dataGridView1.AutoGenerateColumns = false;
            ShelfsdisplayGrid = shefts;
            binding();
                           
        }

        private void binding()
        {
            dataGridView1.DataSource = null;
            for (int i = comboBox1.Items.Count - 1; i >=0; i--)
            {
                comboBox1.Items.RemoveAt(i);
            }
            dataGridView1.DataSource = ShelfsdisplayGrid;
            var groupShelf = shefts.GroupBy(p => p.ShelfName)
                            .OrderBy(p => p.Key);
            foreach (var item in groupShelf)
            {
                comboBox1.Items.Add(item.Key);
            }
            comboBox1.Items.Add("tất cả");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (comboBox1.SelectedItem.ToString() != "tất cả")
            //{
            //    var displaygrid = shefts
            //                      .Where(p => p.ShelfName.Contains(comboBox1.SelectedItem.ToString()))
            //                      .Select(emp => new BusShelf()
            //                      {
            //                          ID = emp.ID,
            //                          ShelfName = emp.ShelfName,
            //                          Isnormal = (bool)emp.Isnormal,
            //                          Tray = emp.Tray
            //                      }
            //                      );
            //    ShelfsdisplayGrid = displaygrid.ToList<BusShelf>();
            //    dataGridView1.DataSource = null;
            //    dataGridView1.DataSource = ShelfsdisplayGrid;
            //}
            //else
            //{
            //    ShelfsdisplayGrid = shefts;
            //    dataGridView1.DataSource = null;
            //    dataGridView1.DataSource = ShelfsdisplayGrid;
            //}
        }

        private void button3_Click(object sender, EventArgs e)
        {
            BusShelf sh = new BusShelf()
            {
                ID = -1,
                Isnormal = true,
                ShelfName = comboBox1.Text,
                Tray = ""
            };
            shefts.Add(sh);
            binding();
            comboBox1.Text = sh.ShelfName;
            Isnotsave();
            dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            foreach (BusShelf item in shefts)
            {
                if (item.ID == -1)
                    item.Insert();
            }
            foreach (BusShelf item in sheftstoupdate)
            {
                item.Update();
            }
            sheftstoupdate = null;
            foreach (BusShelf item in sheftstodelete)
            {
                item.Delete();
            }
            Issaved();
        }

        private void Issaved()
        {
            button2.Text = "Lưu";
        }

        private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            BusShelf updateshelf = ShelfsdisplayGrid[dataGridView1.SelectedRows[0].Index];
            //for (int i = 0; i < shefts.Count - 1; i++)
            //{
            //    if (shefts[i].ID == updateshelf.ID)
            //        shefts[i] = updateshelf;

            //}
            if (updateshelf.ID != -1)
            {
                sheftstoupdate.Add(updateshelf);
            }
            Isnotsave();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("bạn có muốn xóa không", "xác định", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                BusShelf deleteShelf = new BusShelf();
                if ((int)dataGridView1.SelectedRows[0].Cells[2].Value == 0)
                {
                    deleteShelf = ShelfsdisplayGrid[dataGridView1.SelectedRows[0].Index];

                    ShelfsdisplayGrid.Remove(deleteShelf);

                    shefts.Remove(deleteShelf);
                    sheftstodelete.Add(deleteShelf);

                }
                else
                    MessageBox.Show("bạn không thể xóa " + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + dataGridView1.SelectedRows[0].Cells[1].Value.ToString() + " vì còn sản phẩm !!");
                dataGridView1.DataSource = null;
                dataGridView1.DataSource = ShelfsdisplayGrid;
            }
            Isnotsave();

        }

        private void Isnotsave()
        {
            button2.Text = "Lưu *";
        }



    }
}
